package com.mmall.service.impl;

import com.mmall.common.ServiceResponse;
import com.mmall.dao.CategoryMapper;
import com.mmall.pojo.Category;
import com.mmall.service.ICategroyService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by xinxuchu on 2019/3/2.
 */
@Service("iCategroyService")
public class CategroyServiceImpl implements ICategroyService{

    @Autowired
    private CategoryMapper categoryMapper;

    public ServiceResponse addCatagroy(String categroyName,Integer parentid){
        if (parentid == null || StringUtils.isBlank(categroyName)){
            return ServiceResponse.createByError("添加品类参数错误！");
        }
        Category category = new Category();
        category.setName(categroyName);
        category.setParentId(parentid);
        category.setStatus(true);//表示当前节点处于启用状态

        int rowcount = categoryMapper.insert(category);
        if (rowcount>0){
            return ServiceResponse.createBySuccess("添加品类成功！");
        }
        return ServiceResponse.createByError("添加品类失败！");
    }

    public ServiceResponse setCategroyName(String categroyName,Integer categroyid){
        if (categroyid == null || StringUtils.isBlank(categroyName)){
            return ServiceResponse.createByError("更新品类参数错误！");
        }
        Category category = new Category();
        category.setName(categroyName);
        category.setId(categroyid);
        int rowcount = categoryMapper.updateByPrimaryKeySelective(category);
        if (rowcount>0){
            return ServiceResponse.createBySuccess("更新品类成功！");
        }
        return ServiceResponse.createByError("更新品类失败！");
    }
}
